package com.buka.controller;

import com.buka.entity.Result;
import com.buka.entity.StatusCode;
import com.buka.service.CartService;
import com.buka.utils.BaseExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.buka.cart.pojo.Cart;

import java.util.List;

@RestController
@RequestMapping ("/cart")
public class CartController {
    @Autowired
    CartService cartService;

    @PostMapping("/Add")
    public Result add(@RequestBody Cart cart){
        try {
            cartService.add(cart);
        } catch (Exception e) {
            return new BaseExceptionHandler().error("添加失败");
        }
        return new Result<>(true,"添加成功");
    }


    @PostMapping("/delete")
    public Result Delete(@RequestParam String ids){
        try {
            cartService.delete(ids);
        } catch (Exception e) {
            return new BaseExceptionHandler().error("删除失败");
        }
        return new Result<>(true,"删除成功");
    }

    @PostMapping("/findAll")
    public Result<List<Cart>> findAll(){
        List<Cart> all;
        try {
            all = cartService.findAll();
        } catch (Exception e) {
            return new BaseExceptionHandler().error("查询失败");
        }
        return new Result<>(true, StatusCode.OK,"查询成功",all);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Cart cart){
        try {
            cartService.update(cart);
        } catch (Exception e) {
            return new BaseExceptionHandler().error("修改失败");
        }
        return new Result<>(true,"修改成功");
    }
}